﻿using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 类名 : 浮动消息
    /// 作者 : Canyon / 龚阳辉
    /// 日期 : 2024-05-13 15:01
    /// 功能 : 
    /// </summary>
    public class FloatNotice
    {
        static public FloatNotice Instance { get; private set; } = new FloatNotice();

        private FloatNotice()
        {
            this.Init();
        }

        List<string> msgPool = null;

        float lmtCount = 20;
        int cntMaxShow = 3;
        int cntInShow = 0;

        private void Init()
        {
            var lmt = APIUIConst.GetFLoatNoticeLimit();
            this.lmtCount = lmt.Item1;
            int maxShow = lmt.Item2;
            this.cntMaxShow = Mathf.Max(maxShow, 3);
            this.Clear(false);
        }

        private bool IsHasInfo()
        {
            return (this.msgPool != null && this.msgPool.Count > 0);
        }

        public void ShowError(int errCode)
        {
            var cfgOne = TableCenter.errorCode.Get(errCode);
            if (cfgOne != null)
            {
                this.Show(cfgOne.Describe);
            }
            else
            {
                this.Show($"ErrorCode : {errCode}");
            }
        }

        public void Show(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;

            if (this.msgPool == null)
                this.msgPool = ListPool<string>.Get();

            if (this.lmtCount > this.cntMaxShow && msgPool.Count >= this.lmtCount)
                this.msgPool.RemoveAt(msgPool.Count - 1);

            this.msgPool.Add(str);

            if (this.cntInShow >= this.cntMaxShow)
                return;

            this.cntInShow++;

            bool _isShowUI = ManagerCenter.UI.IsWindowShowing<UIFloatNotice>();
            if (_isShowUI)
            {
                this.Notify();
                return;
            }
            this.ViewUI().Forget();
        }

        private async UniTaskVoid ViewUI()
        {
            await ManagerCenter.UI.ShowWindowAsync<UIFloatNotice>();
            this.Notify();
        }

        private void Notify()
        {
            if (IsHasInfo())
            {
                var str = this.msgPool[0];
                this.msgPool.RemoveAt(0);
                MsgDispatcher.Broadcast(MsgEventType.FNotice_ShowInfo, str);
            }
        }

        public bool Next()
        {
            this.cntInShow--;
            bool isOkey = this.IsHasInfo();
            if (isOkey)
                this.Notify();
            return isOkey || this.cntInShow > 0;
        }

        public void Clear(bool release = false)
        {
            bool isHasPool = this.msgPool != null;
            if (isHasPool)
                this.msgPool.Clear();
            if (release)
            {
                if (isHasPool)
                    ListPool<string>.Put(this.msgPool);
                this.msgPool = null;
            }
        }
    }
}
